package com.lanchetech.admin.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lanchetech.admin.service.BlindBoxService;
import com.lanchetech.bean.dto.BlindBoxDto;
import com.lanchetech.bean.dto.BlindBoxPrizeDto;
import com.lanchetech.bean.request.BlindBoxPrizeReq;
import com.lanchetech.bean.vo.BlindBoxPrizeVo;
import com.lanchetech.bean.vo.BlindBoxVo;
import com.lanchetech.bean.request.BlindBoxReq;
import com.lanchetech.bean.response.BasePageResp;
import com.lanchetech.bean.response.BaseResp;
import com.lanchetech.bean.response.ResultData;
import com.lanchetech.bean.util.PageHelpUtil;
import com.lanchetech.bean.vo.TenantInfoVO;
import com.lanchetech.common.enums.ResultCode;
import com.lanchetech.common.exception.ResultCodeException;
import com.lanchetech.common.util.ObjectUtil;
import com.lanchetech.dao.BlindBoxMapper;
import com.lanchetech.dao.BlindBoxPrizeMapper;
import com.lanchetech.dao.MerchantMapper;
import com.lanchetech.dao.TenantMapper;
import com.lanchetech.entity.BlindBox;
import com.lanchetech.entity.BlindBoxPrize;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class BlindBoxServiceImpl implements BlindBoxService {

    @Autowired
    private BlindBoxMapper blindBoxMapper;
    @Autowired
    private BlindBoxPrizeMapper blindBoxPrizeMapper;
    @Autowired
    private TenantMapper tenantMapper;

    @Override
    public BaseResp editBlindBox(BlindBox blindBox) throws ResultCodeException {
        // 添加
        if (blindBox.getId() == null) {
            blindBox.setCreated(new Date());
            BlindBoxDto dto = ObjectUtil.copy(blindBox, BlindBoxDto.class);
            blindBoxMapper.insert(dto);
            return new BaseResp();
        }
        // 编辑
        BlindBox box = blindBoxMapper.selectByPrimaryKey(blindBox.getId());
        // 如果盒子状态是开启  则不允许编辑
        if (box.isWhether()) {
            throw new ResultCodeException(ResultCode.AFTER_OPENING_EDIT);
        }
        blindBox.setStatus((byte) 1);
        blindBox.setUpdated(new Date());
        blindBox.setWhether(false);
        blindBox.setCreated(box.getCreated());
        BlindBoxDto dto = ObjectUtil.copy(blindBox, BlindBoxDto.class);
        blindBoxMapper.updateByPrimaryKeySelective(dto);
        return new BaseResp();
    }

    @Override
    public ResultData<BasePageResp<BlindBoxVo>> getBlindBoxList(BlindBoxReq blindBoxReq) {
        Page page = PageHelper.startPage(blindBoxReq.getPageNo(), blindBoxReq.getPageSize());
        List<BlindBoxVo> blindBoxList = blindBoxMapper.findAll(blindBoxReq);
        return PageHelpUtil.buildPage(blindBoxList, page);
    }

    @Override
    public BaseResp examine(Long id, Byte status) throws ResultCodeException {
        BlindBox blindBox = findOneBlindBoxById(id);
        if (blindBox.isWhether()) {
            throw new ResultCodeException(ResultCode.BLIND_BOX_ENABLED);
        }
        if (blindBox.getStatus() == 2) {
            throw new ResultCodeException(ResultCode.CANNOT_REPEAT);
        }
        blindBox.setId(id);
        blindBox.setStatus(status);
        blindBox.setVerifyTime(new Date());
        blindBoxMapper.updateByPrimaryKeySelective(ObjectUtil.copy(blindBox, BlindBoxDto.class));
        return new BaseResp();
    }

    public BlindBox findOneBlindBoxById(Long id) throws ResultCodeException {
        BlindBox box = blindBoxMapper.selectByPrimaryKey(id);
        if (null == box) {
            throw new ResultCodeException(ResultCode.BLIND_BOX_NULL);
        }
        List<BlindBoxPrize> blindBoxPrizes = blindBoxPrizeMapper.findOneByBlindBoxId(id);
        box.setBlindBoxPrizes(blindBoxPrizes);
        return box;
    }

    @Override
    public BaseResp whether(Long id, Boolean whether) throws ResultCodeException {
        BlindBox box = findOneBlindBoxById(id);
        if (box.isWhether()) {
            throw new ResultCodeException(ResultCode.BLIND_BOX_WHETHER);
        }
        if (box.getStatus() == 1) {
            throw new ResultCodeException(ResultCode.BLIND_BOX_NOT_APPROVED);
        }
        if (CollectionUtils.isEmpty(box.getBlindBoxPrizes())) {
            throw new ResultCodeException(ResultCode.BLIND_BOX_IS_NULL_PRIZE);
        }
        box.setWhether(whether);
        blindBoxMapper.updateByPrimaryKeySelective(ObjectUtil.copy(box, BlindBoxDto.class));
        return new BaseResp();
    }

    @Override
    public BaseResp addPrize(BlindBoxPrize blindBoxPrize) throws ResultCodeException {
        // 盲盒是否存在
        findOneBlindBoxById(blindBoxPrize.getBlindBoxId());
        TenantInfoVO tenantInfoVO = tenantMapper.getTenantInfoById(blindBoxPrize.getTenantId());
        if (tenantInfoVO == null) {
            throw new ResultCodeException(ResultCode.SHOP_NOT_EXIST);
        }
        if (blindBoxPrize.getId() == null) {
            blindBoxPrize.setCreated(new Date());
            blindBoxPrize.setWhether(true);
            BlindBoxPrizeDto dto = ObjectUtil.copy(blindBoxPrize, BlindBoxPrizeDto.class);
            blindBoxPrizeMapper.insert(dto);
            return new BaseResp();
        }
        // 编辑
        blindBoxPrize.setWhether(false);
        blindBoxPrizeMapper.updateByPrimaryKeySelective(ObjectUtil.copy(blindBoxPrize, BlindBoxPrizeDto.class));
        return new BaseResp();
    }

    @Override
    public BaseResp whetherOpenPrize(Long prizeId) throws ResultCodeException {
        BlindBoxPrize blindBoxPrize = blindBoxPrizeMapper.selectByPrimaryKey(prizeId);
        if (blindBoxPrize == null) {
            throw new ResultCodeException(ResultCode.IS_NULL_PRIZE);
        }
        blindBoxPrize.setWhether(!blindBoxPrize.isWhether());
        blindBoxPrizeMapper.updateByPrimaryKey(blindBoxPrize);
        return new BaseResp();
    }

    @Override
    public ResultData<BasePageResp<BlindBoxPrizeVo>> getPrizes(BlindBoxPrizeReq blindBoxPrizeReq) {
        Page page = PageHelper.startPage(blindBoxPrizeReq.getPageNo(), blindBoxPrizeReq.getPageSize());
        List<BlindBoxPrizeVo> blindBoxPrizes = blindBoxPrizeMapper.findAllPrizeByBlindBoxId(blindBoxPrizeReq);
        return PageHelpUtil.buildPage(blindBoxPrizes, page);
    }

    @Override
    public void deleteBlindBoxById(Long id) throws ResultCodeException {
        BlindBox blindBox = findOneBlindBoxById(id);
        if (blindBox.isWhether()) {
            throw new ResultCodeException(ResultCode.ACTIVITY_UNABLE_TO_DELETE);
        }
        if (blindBox.isDeleted()) {
            throw new ResultCodeException(ResultCode.FAIL);
        }
        // 删除盲盒
        blindBox.setDeleted(true);
        blindBoxMapper.updateByPrimaryKeySelective(ObjectUtil.copy(blindBox, BlindBoxDto.class));
        // 删除奖项
        if (CollectionUtils.isNotEmpty(blindBox.getBlindBoxPrizes())) {
            List<Long> ids = new ArrayList<>();
            blindBox.getBlindBoxPrizes().forEach(blindBoxPrize -> ids.add(blindBoxPrize.getId()));
            blindBoxPrizeMapper.deleteByIds(ids, true);
        }

    }
}
